import * as L from "leaflet";

export default function initMap(containerId, centralPoint, zoom, zoomSnap) {
  L.TileLayer.ChinaProvider = L.TileLayer.extend({
    initialize: function (type, options) { // (type, Object)
      const providers = L.TileLayer.ChinaProvider.providers;
      const parts = type.split('.');
      const providerName = parts[0];
      const mapName = parts[1];
      const mapType = parts[2];
      const url = providers[providerName][mapName][mapType];
      options.subdomains = providers[providerName].Subdomains;
      L.TileLayer.prototype.initialize.call(this, url, options);
    }
  });

  const imgParam = "&SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&FORMAT=tiles";
  const ciaParam = "&SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cia&STYLE=default&TILEMATRIXSET=w&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&FORMAT=tiles";

  const tk = "a4d5ae97892a87f004ed1028db28eaf2"
  L.TileLayer.ChinaProvider.providers = {
    TianDiTu: {
      Satellite: {
        Map: "https://t{s}.tianditu.gov.cn/img_w/wmts?tk=" + tk + imgParam,
        Annotation: "https://t{s}.tianditu.gov.cn/cia_w/wmts?tk=" + tk + ciaParam
      },
      Subdomains: ['0', '1', '2', '3', '4', '5', '6', '7']
    },
  };


  L.tileLayer.chinaProvider = function (type, options) {
    return new L.TileLayer.ChinaProvider(type, options);
  };

  /**
   * 天地图内容
   */
  const imga = L.tileLayer.chinaProvider('TianDiTu.Satellite.Annotation', {
      maxZoom: 26,
      minZoom: 1,
    });
  imga.bringToFront()


  //题图titleLayer
  const ia = L.layerGroup([imga])
  let map
  if (zoomSnap !== undefined) {
    map = L.map(containerId, {
      editable: false,
      center: centralPoint,
      editOptions: {},
      zoom: zoom,
      layers: [ia],
      zoomSnap: zoomSnap,
      closePopupOnClick: false,
      crs: L.CRS.EPSG3857,
      attributionControl: false,

    });
  } else {
    map = L.map(containerId, {
      editable: true,
      center: centralPoint,
      editOptions: {},
      zoom: zoom,
      layers: [ia],
      zoomControl: false,
      closePopupOnClick: false,
      crs: L.CRS.EPSG3857,
      // 去掉比列尺
      attributionControl: false,
    });
  } 
  ia.setZIndex(100000)
  return {
    "map": map,
  };
}
